<?php

namespace App\Containers\AppSection\Pledge\Tasks;

use App\Containers\AppSection\Pledge\Data\Repositories\PledgeRepository;
use App\Ship\Criterias\ThisProjectCriteria;
use App\Ship\Exceptions\DeleteResourceFailedException;
use App\Ship\Parents\Tasks\Task;
use Exception;

class DeletePledgesTask extends Task
{
    protected PledgeRepository $repository;

    public function __construct(PledgeRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run($id = null): ?int
    {
        try {
            if (empty($id))
                return $this->repository->deleteWhere([]);
            else
                return $this->repository->delete($id);
        } catch (Exception $exception) {
            throw new DeleteResourceFailedException();
        }
    }

    public function thisProject($projectId): self
    {
        $this->repository->pushCriteria(new ThisProjectCriteria($projectId));
        return $this;
    }
}
